package com.qbm.association.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qbm.association.bean.*;
import com.qbm.association.config.Audience;
import com.qbm.association.entity.*;
import com.qbm.association.mapper.ActivityMapper;
import com.qbm.association.service.*;
import com.qbm.association.util.JWTTokenUtil;
import io.jsonwebtoken.lang.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Aron
 * @since 2022-02-07
 */
@RestController
@RequestMapping("/association/activity")
public class ActivityController {

    @Resource
    private ActivityService activityService;

    @Resource
    private JoinActivityService joinActivityService;

    @Resource
    private ActivityMapper activityMapper;

    @Resource
    private Audience audience;

    @Resource
    private StudentService studentService;

    @Resource
    private ClubService clubService;

    @Resource
    private AdministratorService administratorService;




    @GetMapping("getPageActivity")
    public ResultBean getPageActivity(Integer status, Integer currentPage, String activityName) {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        if (status != -1) {
            queryWrapper.eq("activity_status", status);
        }
        if (activityName != null && activityName.length() != 0) {
            queryWrapper.like("name", activityName);
        }
        Page<Activity> page = new Page<>(currentPage, 5);
        Page<Activity> page1 = activityMapper.selectPage(page, queryWrapper);
        return ResultBean.success(page1, "获取成功");
    }

    /**
     *
     * @param activity
     * @return
     */
    @PostMapping("auditActivity")
    public ResultBean auditActivity(Activity activity, HttpServletRequest request) {
        String token = request.getHeader(JWTTokenUtil.AUTH_HEAD_KEY);
        String username = JWTTokenUtil.getUserName(token, audience.getBase64Secret());
        Administrator byUsername = administratorService.getByUsername(username);
        activity.setVerifierId(byUsername.getId());
        boolean isTrue = activityService.updateById(activity);
        Assert.isTrue(isTrue, "审核失败");

        // 保存joinActivity
        Activity activity1 = activityService.getById(activity.getId());
        JoinActivity joinActivity = new JoinActivity();
        joinActivity.setClubId(activity1.getClubId());
        joinActivity.setUserId(activity1.getCreateUserId());
        joinActivity.setActivityId(activity.getId());
        joinActivity.setUserPower(JoinActivityPowerEnum.MANAGER.getCode());
        joinActivity.setJoinActivityStatus(JoinActivityStatusEnum.SUCCESS.getCode());
        joinActivityService.save(joinActivity);
        return ResultBean.success("审核成功");
    }

    @PostMapping("addActivity")
    public ResultBean addActivity(Activity activity, HttpServletRequest request) {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", activity.getName());
        boolean isTrue = activityService.list(queryWrapper).size() == 0;
        Assert.isTrue(isTrue, "该活动已存在");

        activity.setCreateUserId(studentService.getByUsername(JWTTokenUtil.getUserName
                (request.getHeader(JWTTokenUtil.AUTH_HEAD_KEY), audience.getBase64Secret())).getId());
        activity.setActivityStatus(ActivityStatusEnum.WAITING.getCode());
        activity.setClubName(clubService.getById(activity.getClubId()).getName());
        boolean isTrue1 = activityService.save(activity);
        Assert.isTrue(isTrue1, "申请失败");
        return ResultBean.success("申请成功，待审核");
    }

    @GetMapping("getMyManageActivityPageList")
    public ResultBean getMyManageActivityPageList(Integer currentPage, Integer clubId, String activityName) {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("club_id", clubId);
        if (activityName != null && activityName.length() > 0) {
            queryWrapper.like("name", activityName);
        }
        Page<Activity> page = new Page<>(currentPage, 5);
        Page<Activity> page1 = activityMapper.selectPage(page, queryWrapper);
        return ResultBean.success(page1, "获取成功");
    }

    @GetMapping("getActivityMember")
    public ResultBean getActivityMember(Integer activityId) {
        QueryWrapper<JoinActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id", activityId)
                .eq("join_activity_status",JoinActivityStatusEnum.SUCCESS.getCode());
        List<JoinActivity> joinActivityList = joinActivityService.list(queryWrapper);
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        for (int i =0;i<joinActivityList.size();i++){
            studentQueryWrapper.eq("id",joinActivityList.get(i).getUserId()).or();
        }
        Map<String,Object> map = new HashMap<>();
        List<Student> list = studentService.list(studentQueryWrapper);
        map.put("stuList",list);
        map.put("num",list.size()+"/"+activityService.getById(activityId).getMaxPerson());
        return ResultBean.success(map, "获取成功");
    }

    /**
     * 社团主页，分页社团活动
     * @param currentPage
     * @param clubId
     * @return
     */
    @GetMapping("getClubActivity")
    public ResultBean getClubActivity(Integer currentPage, Integer clubId) {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("activity_status",1);
            queryWrapper.eq("club_id",clubId);
        Page<Activity> page = new Page<>(currentPage,10);
        Page<Activity> page1 = activityMapper.selectPage(page, queryWrapper);
//        List<Activity> activities = activityMapper.selectList(queryWrapper);
        return ResultBean.success(page1,"获取成功");
    }

    @GetMapping("getClubActivityList")
    public ResultBean getClubActivityList(Integer clubId) {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_status",1);
        queryWrapper.eq("club_id",clubId);
        List<Activity> activityList = activityService.list(queryWrapper);
        return ResultBean.success(activityList,"获取成功");
    }
    @PostMapping("updateActivity")
    public ResultBean updateActivity( Activity activity) {
        activity.setActivityStatus(ActivityStatusEnum.WAITING.getCode());
        boolean isTrue = activityService.updateById(activity);
        Assert.isTrue(isTrue, "更新失败");
        return ResultBean.success("操作成功");
    }
}


